package com.example.demo100.web;

import com.example.demo100.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName IndexController
 * @Description TODO
 * @Author HP
 * @Date 20:34 2022/7/14
 * @Version 1.0
 **/
@Controller
@RequestMapping()
public class IndexController {

    public final static List<User> userList = new ArrayList<>();

    @GetMapping
    public String login(){
        return "login";
    }

    @PostMapping("login")
    public String login(User user){

        if(user==null||userList.stream().filter(u -> u.getUsername().equals(user.getUsername())).count()==0){
            return "regist";
        }
        List<User> collect = userList.stream().filter(u -> u.getUsername().equals(user.getUsername()) && u.getPassword().equals(user.getPassword())).collect(Collectors.toList());
       if(collect.size()!=0){
           return "home";
       }
        return "login";
    }

    @PostMapping("regist")
    public String regist(User user){
        if(user!=null&&user.getUsername()!=null&&user.getPassword()!=null){
            userList.add(user);
            return "login";
        }
        return "regist";
    }
}
